package mx.com.sinformex.simbel.utils.export;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.tapestry5.StreamResponse;
import org.apache.tapestry5.services.Response;

import mx.com.sinformex.simbel.utils.IFileReporteExporter;

/**
 * @author Oscar Ivan Hernandez Ventura [ oscar at sinformex dot com dot mx]
 *
 * @version $ Id : XlsExporter.java , v 1.0 21/05/2009 angellore $
 */
public class XlsExporter implements IFileReporteExporter {
	
	private final int START_ROW = 5;
	private final int START_COLUMN = 2;

	public StreamResponse export(List<Map<String, Object>> rows,
			String templateFileName, Map<String, Object> args, Connection conn,
			String outputFilename) {

		HSSFWorkbook book = new HSSFWorkbook();
		HSSFSheet sheet = book.createSheet();
		int currentRow = START_ROW;
		boolean header = false;
		
		//Do header
		for(Map<String, Object> row : rows) {
			
			if(!header) {
				HSSFRow fila = sheet.createRow(currentRow++);
				int currentColumn = START_COLUMN;
				for(Iterator<String> iter = row.keySet().iterator() ; iter.hasNext() ; ) {
					String key = iter.next();

					HSSFCell celda = fila.createCell((short) currentColumn++);
					HSSFRichTextString texto = new HSSFRichTextString(key);
					celda.setCellValue(texto);
				}
				header = true;
			}
			
			HSSFRow fila = sheet.createRow(currentRow++);
			int currentColumn = START_COLUMN;			

			for(Iterator<String> iter = row.keySet().iterator() ; iter.hasNext() ; ) {
				String key = iter.next();
				HSSFCell celda = fila.createCell((short) currentColumn++);
				HSSFRichTextString texto = new HSSFRichTextString((String) row.get(key));
				celda.setCellValue(texto);
			}
					
		}
		/*
		try {
			FileOutputStream out = new FileOutputStream(new File("/Users/angellore/Desktop/prueba.xls"));
			book.write(out);
			out.flush();
			out.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}*/
		
		return new XlsStreamResponse(new ByteArrayInputStream(book.getBytes()), outputFilename);
	}
	
	class XlsStreamResponse implements StreamResponse {
		
		private final String CONTENT_TYPE = "application/vnd.ms-excel";
		private InputStream in;
		private String filename ;
		private final String EXTENSION = ".xls";	
		
		public XlsStreamResponse(InputStream in , String filename) {
			this.in = in;
			this.filename = filename;
		}

		public String getContentType() {
			return CONTENT_TYPE;
		}

		public InputStream getStream() throws IOException {
			return in;
		}

		public void prepareResponse(Response response) {
			response.setHeader("Content-Disposition", "attachment; filename=" + filename + EXTENSION);
			response.setHeader("Expires", "0");
			response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
			response.setHeader("Pragma", "public");				
		}
		
	}

}
